import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;


public class TestFooBean {

	private static SerializeConfig mappingConfig = new SerializeConfig();
	
	static{
		mappingConfig.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
	//add a customized serializer for the Date class	
	}
	
	public static final byte[] toJSONStringBytes(Object object, com.alibaba.fastjson.serializer.SerializerFeature... features) {
	    SerializeWriter out = new SerializeWriter();
	 
	    try {
	        JSONSerializer serializer = new JSONSerializer(out);
	        for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
	            serializer.config(feature, true);
	        }
	 
	        serializer.write(object);
	 
	        return out.toBytes("UTF-8");
	    } catch (StackOverflowError e) {
	        throw new JSONException("maybe circular references", e);
	    } finally {
	        out.close();
	    }
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		FooBean foo = new FooBean();
		Date date = new Date();
		String jsonString = JSON.toJSONString(date, mappingConfig);
		System.out.println(jsonString);
		System.out.println(JSON.toJSONString(foo, true));
		
		String jsonFooString = JSON.toJSONString(foo);
		System.out.println(jsonFooString.length());
		byte[] jsonFooBytes = JSON.toJSONBytes(foo, SerializerFeature.WriteClassName);
		byte[] jsonFooBytes2 = toJSONStringBytes(foo, SerializerFeature.WriteClassName);
		System.out.println(jsonFooBytes.length);
		System.out.println(jsonFooString);
		System.out.println(new String(jsonFooBytes));
		System.out.println(new String(jsonFooBytes2));
		
		json2Map();
		map2JSON();
		array2JSON();
		System.out.println("\narray2Json2()");
		array2JSON2();
		
	}
	
	public static void json2Map(){ 
        //Map -> JSON 
        Map<String,Bar> map = new HashMap<String, Bar>();
        map.put("a",new Bar()); 
        map.put("b",new Bar()); 
        map.put("c",new Bar()); 
        String json = JSON.toJSONString(map,false); 
        System.out.println(json); 
        //JSON -> Map 
        Map<String,Bar> map1 = (Map<String,Bar>)JSON.parse(json); 
        for (String key : map1.keySet()) { 
                System.out.println(key+":"+map1.get(key)); 
        } 
} 

public static void array2JSON(){ 
        String[] arr_String    = {"a","b","c"}; 
        String json_arr_String = JSON.toJSONString(arr_String,true); 
        System.out.println(json_arr_String); 
        JSONArray jsonArray = JSON.parseArray(json_arr_String); 
        for (Object o : jsonArray) { 
                System.out.println(o); 
        } 
        System.out.println(jsonArray); 
} 
public static void array2JSON2(){ 
        Bar[] arrayBars    = {new Bar(),new Bar(),new Bar()}; 
        String jsonArrayBars = JSON.toJSONString(arrayBars,false); 
        System.out.println(jsonArrayBars); 
        JSONArray jsonArray = JSON.parseArray(jsonArrayBars); 
        for (Object o : jsonArray) { 
                System.out.println(o); 
        } 
        System.out.println(jsonArray); 
} 

public static void map2JSON(){ 
        Map map = new HashMap<String, String>(); 
        map.put("a","aaa"); 
        map.put("b","bbb"); 
        map.put("c","ccc"); 
        String json=JSON.toJSONString(map); 
        System.out.println(json); 
        Map map1 = JSON.parseObject(json); 
        for (Object o : map.entrySet()) { 
                Map.Entry<String,String> entry = (Map.Entry<String,String>)o; 
                System.out.println(entry.getKey()+"--->"+entry.getValue()); 
        } 
} 
}

